package com.j3l11234.familyvote.core;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class Core {
  public void start() {
    DecimalFormat df = new DecimalFormat("00");

    CloseableHttpClient httpclient = HttpClients.createDefault();


    HttpGet httpkeepLive = new HttpGet("http://s4.foo.vutimes.com:3060/game?cmd=keepLive&click=60&kill=0&token=nya6VUyVWzmBnfxscr7qIAuu");
    Core.setHeader(httpkeepLive);

    //HttpGet httpuseblitz = new HttpGet("http://s4.foo.vutimes.com:3060/game?cmd=useblitz&type=0&token=nya6VUyVWzmBnfxscr7qIAuu");
    //test.setHeader(httpuseblitz);       

    while (true) {
      try {
        CloseableHttpResponse response1 = httpclient.execute(httpkeepLive);
        //System.out.println(response1.getStatusLine());
        String jsonText = EntityUtils.toString(response1.getEntity());
        response1.close();


        System.out.println(jsonText);
        JSONObject jsonObject = new JSONObject(jsonText);
        Map<String,Object> result = new HashMap();
        Iterator<String> iterator = jsonObject.keys();
        String key = null;
        while (iterator.hasNext()) {
          key = iterator.next();
          result.put(key, jsonObject.get(key));
        }
        BigDecimal ans = new BigDecimal("1000000000000000000000000");
        BigDecimal total = new BigDecimal(result.get("total").toString());
        BigDecimal rate = new BigDecimal(result.get("rate").toString());
        Long leftTime = ans.subtract(total).divide(rate,0).longValue();

        long day = leftTime/86400;
        String hour = df.format((leftTime % 86400)/3600);
        String min = df.format((leftTime % 3600)/60);
        String sec = df.format(leftTime % 60);
        System.out.println(day+"-"+hour+":"+min+":"+sec);

      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    //CloseableHttpResponse response1 = httpclient.execute(httpuseblitz);
    //System.out.println(response1.getStatusLine());
    //HttpEntity entity1 = response1.getEntity();
    //System.out.println(EntityUtils.toString(entity1));
    //response1.close();
    //Thread.sleep(1000);



  }

  public static void setHeader(HttpGet httpGet){
    httpGet.setHeader("Host","s4.foo.vutimes.com:3060");
    httpGet.setHeader("Connection","keep-alive");  
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 4.4.4; MI 2 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 MicroMessenger/6.1.0.66_r1062275.542 NetType/WIFI");  
    httpGet.setHeader("Origin","http://foo.ygyscc.info");  
    httpGet.setHeader("Accept","*/*"); 
    httpGet.setHeader("Referer", "http://foo.ygyscc.info/game.html?v=9116");           
    httpGet.setHeader("Accept-Encoding","gzip,deflate");
    httpGet.setHeader("Accept-Language","en-US");
    httpGet.setHeader("X-Requested-With","com.tencent.mm");
  }
}
